<?php
class Controller_Admin_Activity extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_no_buttons = TRUE;
        $this->_list_pagesize = 10;
        $this->_form_type  = 'activity';

        parent::__construct($req, $res);

        $this->_list_where = array(
            array('status', '=', 'VALID'),
        );
    }

    public function action_index ()
    {
        $this->action_list();
    }

    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 

        $this->_form_data = $data;
        parent::action_detail($nid);
    }

    public function action_category ($nid)
    {
        $post = $this->request->post();
        if (empty($post))
        {
            $this->template->set_filename('smarty:www/activity/admin_category');

            $cates = App_Category::all_categories(0, 0, FALSE, 3);
            $this->template->set('cates', $cates);

            $act   = new App_Activity($nid);
            $detail= $act->detail();
            $this->template->set('activity', $detail);

            $relation = App_CategoryGL::listByTarget('ACTIVITY', $nid);
            $catIds   = array();
            foreach ($relation as $rs)
            {
                $catIds[] = $rs['categoryId'];
            }

            $this->template->set('catIds', $catIds);
        }
        else
        {
            $cids = Arr::get($post, 'cid');
            App_CategoryGL::setTarget(user::current_user_id(), 'ACTIVITY', $nid, $cids);
            $this->to_url('list');
        }
    }

    private function rebuild_post_data (& $post)
    {
        //
        $post['managerUid'] = user::current_user_id();

    }

    public function list_get_data ($offset, $limit)
    {
        $track = new App_Activity; 
        list($count, $tracks) = $track->search(NULL, $offset, $limit);

        $this->_list_total_size = $count;
        return $tracks;
    }

    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            $rs['guiderName'] = $rs['guider']['realName'];
            $price = $rs['priceService'];
            $rs['price'] = number_format($price/100, 2) . ' €/天';
        }
    }


    public function classname_by_list_row (& $row)
    {
    }

    public function actions_by_list_row (& $row, & $menus)
    {
	    $controller = Request::initial()->controller();
	    $directory  = Request::initial()->directory();
	    $directory  = empty($directory) ? '' : '/'.$directory;

        unset($menus[1]);
        unset($menus[0]);
        array_unshift($menus, '<a target="_blank" href="/activity/detail/'.$row['id'].'">查看</a>');
        $menus[] = '<a href="/admin/activity/category/'.$row['id'].'">分类</a>';
    }

    public function action_delete ($nid)
    {
        $track = new App_Activity($nid);
        $track->remove();
        $this->to_url('list');
    }

    public function after_update ($nid)
    {
    }

    public function after_create ($nid)
    {
    }
}


